home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Sample LDEFs 2.0 / sicn LDEF (Fixed) / sicn ldef.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  3.3 KB  |  131 lines  |  [TEXT/KAHL]

  1. /*    SICN LDEF
  2.  
  3.     ©1991 Apple Computer, Inc.
  4.     written by Steven Falkenburg 5/23/91
  5.     This LDEF displays small icons to the left of text in a list.
  6.     
  7.     The small icon is stored in the first 32 bytes of each cell.
  8.     
  9.     modified by Matt Slot, 9/6/93
  10.         Fixed odd address problem in the SICN plotting
  11.  
  12.     modified by Matt Slot, 6/27/95
  13.         Changed the Hilite Mode to use accessor functions
  14.         Properly save and restore Handle states
  15. */
  16.  
  17.  
  18. /* constants for spacing */
  19.  
  20. #define kLeftOffset    2
  21. #define kTopOffset    0
  22. #define kIconSpace    2
  23.  
  24. // Universal Headers users can replace these with LM accessor functions
  25. pascal unsigned char GetHiliteMode(void) = { 0x1EB8, 0x0938 }; /* MOVE.B 0x0938,(A7) */
  26. pascal void SetHiliteMode(unsigned char) = { 0x11DF, 0x0938 }; /* MOVE.B (A7)+,0x0938 */
  27.  
  28.  
  29. /* prototypes */
  30.  
  31. void DrawSICN(Ptr theSICN,short left,short top,GrafPtr drawPort);
  32.  
  33. /* main LDEF entry point */
  34.  
  35. pascal void    main(short lMessage,Boolean lSelect,Rect *lRect,Cell lCell,
  36.                 short lDataOffset,short lDataLen,ListHandle lHandle)
  37. {
  38.     FontInfo fontInfo;                        /* font information (ascent/descent/etc) */
  39.     ListPtr listPtr;                        /* pointer to store dereferenced list */
  40.     SignedByte hStateList,hStateCells;        /* state variables for HGetState/SetState */
  41.     Ptr cellData;                            /* points to start of cell data for list */
  42.     Ptr theSICN;                            /* points to SICN to be drawn */
  43.     short leftDraw,topDraw;                    /* left/top offsets from topleft of cell */
  44.     
  45.     #pragma unused (lCell)
  46.     
  47.     theSICN = NewPtr(32);
  48.     
  49.     /* lock and dereference list mgr handles */
  50.     
  51.     hStateList = HGetState((Handle) lHandle);
  52.     HLock((Handle) lHandle);
  53.     listPtr = *lHandle;
  54.     hStateCells = HGetState(listPtr->cells);
  55.     HLock(listPtr->cells);
  56.     cellData = *(listPtr->cells);
  57.     
  58.     switch (lMessage) {
  59.       case lInitMsg:
  60.           /* we don't need any initialization */
  61.           break;
  62.  
  63.       case lDrawMsg:
  64.         EraseRect(lRect);
  65.         
  66.           if (lDataLen > 0) {
  67.           
  68.               /* determine starting point for drawing */
  69.               
  70.               leftDraw =    lRect->left+listPtr->indent.h+kLeftOffset;
  71.               topDraw =    lRect->top+listPtr->indent.v+kTopOffset;
  72.               
  73.               /* plot SICN (first 32 bytes) */
  74.               
  75.               if (lDataLen > 32) {
  76.                   BlockMove(cellData+lDataOffset, theSICN, 32);
  77.                   DrawSICN(theSICN,leftDraw,topDraw,listPtr->port);
  78.                   lDataOffset += 32;
  79.                   lDataLen -= 32;
  80.               }
  81.               leftDraw += 16+kIconSpace;
  82.               
  83.               /* plot text (offset 32 bytes onward) */
  84.               
  85.             GetFontInfo(&fontInfo);
  86.             MoveTo(leftDraw,topDraw+fontInfo.ascent);
  87.             
  88.             /* set condensed mode if necessary (if the text doesn't fit otherwise) */
  89.             
  90.             TextFace(0);
  91.             if (TextWidth(cellData,lDataOffset,lDataLen) > (lRect->right - leftDraw))
  92.                 TextFace(condense);
  93.  
  94.             DrawText(cellData,lDataOffset,lDataLen);
  95.           }
  96.  
  97.         if (!lSelect)
  98.               break;
  99.         
  100.       case lHiliteMsg:
  101.           /* do hilite color */
  102.           SetHiliteMode(GetHiliteMode() ^ (1L << hiliteBit));    
  103.           InvertRect(lRect);
  104.           break;
  105.  
  106.       case lCloseMsg:
  107.           break;
  108.     }
  109.     
  110.     DisposePtr(theSICN);
  111.     HSetState(listPtr->cells,hStateCells);
  112.     HSetState((Handle) lHandle,hStateList);
  113. }
  114.  
  115.  
  116. /* this procedure draws a small icon using CopyBits */
  117.  
  118. void DrawSICN(Ptr theSICN,short left,short top,GrafPtr drawPort)
  119. {
  120.     BitMap iconMap;
  121.     Rect destRect;
  122.  
  123.     iconMap.baseAddr = theSICN;
  124.     iconMap.rowBytes = 2;
  125.     SetRect(&iconMap.bounds,0,0,16,16);
  126.     SetRect(&destRect,0,0,16,16);
  127.     OffsetRect(&destRect,left,top);
  128.     CopyBits(&iconMap,&drawPort->portBits,&iconMap.bounds,&destRect,
  129.             srcCopy,nil);
  130. }
  131.